//
//  ZCreateWalletController.m
//  NewProject
//
//  Created by Apple on 2018/5/18.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZCreateWalletController.h"
#import "ZMainController.h"
#import "ZWHtmlController.h"
#import "ZImportPrivateController.h"
@interface ZCreateWalletController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *titleBackView;
@property (weak, nonatomic) IBOutlet UIButton *importWalletBtn;

@end

@implementation ZCreateWalletController
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    return YES;
}
- (IBAction)importWalletAction:(UIButton *)sender {
//    [self.navigationController pushViewController:[PKSetUserMessageController new] animated:YES];
    [self.navigationController pushViewController:[ZImportPrivateController new] animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)initUI{
    for (UIView *segmentView in self.segmentViews) {
        segmentView.backgroundColor = COLOR_LIGHT_GRAY;
    }
    self.titleBackView.backgroundColor = [UIColor colorWithHexString:@"#076bb6"];
    
    [self.create_wallet_btn setBackgroundColor:RGBCOLOR(214, 214, 214)];
    self.create_wallet_btn.layer.cornerRadius = 5;
    self.create_wallet_btn.clipsToBounds = YES;
    self.create_wallet_btn.titleLabel.font = H_Medium2;
    [self.agree_btn setTitleColor:[UIColor colorWithHexString:@"#4e5e6e"] forState:UIControlStateNormal];
//    self.agree_btn setImage:[UIImage imageNamed:@""] forState:ui
    [self.protocol_btn setTitleColor:[UIColor colorWithHexString:@"#37b9c9"] forState:UIControlStateNormal];
    
    [self.first_warn_btn setTitle:Z_Location(@"passwordStrengthIsCriticalToGuardYourWallet") forState:UIControlStateNormal];
    [self.second_warn_btn setTitle:Z_Location(@"weCanRecoverThePasswordPleaseBackupCautiously") forState:UIControlStateNormal];
    
    self.wallet_name_tf.placeholder = Z_Location(@"walletName");
    self.password_tf.placeholder = Z_Location(@"password");
    self.repeat_password_tf.placeholder = Z_Location(@"repeatPassword");
    [self.agree_btn setTitle:Z_Location(@"agreeOn") forState:UIControlStateNormal];
    [self.protocol_btn setTitle:Z_Location(@"termsOfServiceAndPrivacyPolicy") forState:UIControlStateNormal];
    [self.create_wallet_btn setTitle:Z_Location(@"createWallet") forState:UIControlStateNormal];
    self.create_wallet_btn.userInteractionEnabled = NO;
    [self.importWalletBtn setTitle:Z_Location(@"importWallet") forState:UIControlStateNormal];
    [self.importWalletBtn setTitleColor:[UIColor colorWithHexString:@"#37b9c9"] forState:UIControlStateNormal];
    [ZDBManager queryWallets:0 completion:^(NSArray *wallets) {
        if (wallets.count) {
            self.importWalletBtn.hidden = YES;
        }else
        {
            self.importWalletBtn.hidden = NO;
        }
    }];
    
}
- (IBAction)createWalletAction:(UIButton *)sender {
    BOOL canCreate = NO;
    if (self.wallet_name_tf.text.length != 0 &&self.password_tf.text.length != 0 &&self.repeat_password_tf.text.length != 0) {
        if ([self.password_tf.text isEqualToString:self.repeat_password_tf.text]) {
            canCreate = YES;
        }else
        {
            canCreate = NO;
        }
    }

    if (canCreate) {
        ZWWeakSelf;
        
        WewalletsdkWeWallet *wallet = [ZGRPC_Manager createWallet];
        if (wallet) {
            [ZDBManager createWallet:self.wallet_name_tf.text password:self.password_tf.text address:wallet.address privateKey:wallet.privateKey complition:^(BOOL exist, BOOL addressExist, BOOL isFirst,BOOL creatSuccess) {
                if (exist) {
                    [ZProgressManager showError:Z_Location(@"duplicateWalletName")];
                    return ;
                }
                if (addressExist) {
                    [ZProgressManager showSuccess:Z_Location(@"z_wallet_exist")];
                    return;
                }
                
                if (!creatSuccess) {
                    [ZProgressManager showError:Z_Location(@"createWalletFail")];
                    return;
                }
              
                if (isFirst) {
                    [UIApplication sharedApplication].keyWindow.rootViewController = [ZMainController new];
                }else
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"z_wallet_pais_change" object:nil];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }
                
            }];
        }else
        {
            [ZProgressManager showError:Z_Location(@"createWalletFail")];
            
        }
        
    }else
    {
          [ZProgressManager showError:Z_Location(@"Pleasechecktheparameters")];
    }
}



- (IBAction)entryProtocolAction:(UIButton *)sender {
    ZWHtmlController *htmlControl = [ZWHtmlController new];
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:Z_Location(@"protocolArticle")
                                                         ofType:@"html"];
    NSString * htmlStr = [NSString stringWithContentsOfFile:htmlPath
                                                   encoding:NSUTF8StringEncoding
                                                      error:nil];
    
    htmlControl.urlStr = htmlStr;
    htmlControl.titleStr = Z_Location(@"protocal");
    [self.navigationController pushViewController:htmlControl animated:YES];
}
- (IBAction)agreeProtocolAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.selected) {
        self.create_wallet_btn.backgroundColor = [UIColor colorWithHexString:@"#37b9c9"];
        self.create_wallet_btn.userInteractionEnabled = YES;
        
    }else
    {
        self.create_wallet_btn.backgroundColor = RGBCOLOR(214, 214, 214);
        self.create_wallet_btn.userInteractionEnabled = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = Z_Location(@"createWallet");
    [self initUI];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
